//
//  EditAnswerController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/23.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "EditAnswerController.h"
#import "ReleaseTextAndImageController.h"


@interface EditAnswerController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**描述输入框*/
@property (nonatomic,strong)PubulicTextView*bottomTextView;
/**<#属性#>*/
@property (nonatomic,strong)UICollectionView*listView;
/**<#属性#>*/
@property (nonatomic,strong) UICollectionViewFlowLayout * layout;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataArray;

/**导航栏左边文字按钮*/
@property (nonatomic,strong)UIButton * rightBtn;

@end

@implementation EditAnswerController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"编辑答案";
    [self setNavItems];
    [self createUI];
    
    self.bottomTextView.placeholderText.hidden = YES;
    self.bottomTextView.textView.text = @"一般我炒的时候，是先将鸡蛋打散，下锅炒至7成熟，盛出，倒油放西红柿翻炒至7成熟，导入刚刚炒好的鸡蛋，一起翻炒，出锅。 希望对你有用~";

}

-(void)setNavItems{
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setTitleColor:UIColorFromRGB(0x273323) forState:UIControlStateNormal];
    leftBtn.titleLabel.font = SYSTEMFONT(16);
    
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightBtn setTitle:@"发布" forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateNormal];
    self.rightBtn.titleLabel.font = SYSTEMFONT(16);
//    kWeakSelf(weakSelf);
    [self.rightBtn addActionHandler:^{
        
    }];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBtn];
    
    
}
-(void)createUI{
    [self.view addSubview:self.bottomTextView];
    [self.view addSubview:self.listView];
    
    [self.bottomTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(24);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(100);
    }];
    
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(self.bottomTextView.mas_bottom).offset(20);
        make.right.mas_equalTo(-16);
        make.bottom.mas_equalTo(0);
    }];
}
#pragma mark ---UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count + 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ReleaseImageItem * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(ReleaseImageItem.class) forIndexPath:indexPath];
    if (indexPath.row == self.dataArray.count) {
        cell.isAdd = YES;
    }else{
        cell.model = self.dataArray[indexPath.row];
        cell.isAdd = NO;
        kWeakSelf(weakSelf);
        cell.deleBlck = ^{
            DLogFunc;
            [weakSelf.dataArray removeObjectAtIndex:indexPath.row];
            [weakSelf.listView reloadData];
        };

    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.dataArray.count ) {
        kWeakSelf(weakSelf);
//        [PhotosManager showPhotosManager:self withMaxImageCount:10 withAlbumArray:^(NSMutableArray<PhotoModel *> *albumArray) {
//            [weakSelf.dataArray addObjectsFromArray:albumArray];
//            [weakSelf.listView reloadData];
//        }];
        [PhotosManager showPhotosManager:self withMaxImageCount:9 withAlbumArray:^(NSMutableArray<PhotoModel *> * _Nonnull albumArray) {
            DLog(@"%@", albumArray);
            [weakSelf.dataArray addObjectsFromArray:albumArray];
            [weakSelf.listView reloadData];
        } withCameraImage:^(UIImage * _Nonnull cameraImage) {
            PhotoModel * model = [[PhotoModel alloc] init];
            model.highDefinitionImage = cameraImage;
            [weakSelf.dataArray addObject:model];
            [weakSelf.listView reloadData];
        }];
    }
}
#pragma mark  定义每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (KScreenWidth - 42)/3;
    return CGSizeMake(width, width);
}
// 列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}
// 行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

-(PubulicTextView*)bottomTextView{
    if(!_bottomTextView){
        _bottomTextView=[PubulicTextView new];
        _bottomTextView.placeholderText.text = @"添加描述和配图（选填）";
        _bottomTextView.placeholderText.textColor = UIColorFromRGB(0x959E97);
        _bottomTextView.placeholderText.font = SYSTEMFONT(14);
        _bottomTextView.backgroundColor = KRedColor;
        _bottomTextView.textView.font = BOLDSYSTEMFONT(14);
        _bottomTextView.textView.textColor=UIColorFromRGB(0x454F44);
    }
    return _bottomTextView;
}
-(UICollectionView *)listView{
    if (!_listView) {
        self.layout= [[UICollectionViewFlowLayout alloc] init];
        _listView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
//        CGRectMake(16, 140, KScreenWidth-32, KScreenHeight-K_TopHeight-36-140)
        _listView.backgroundColor=KWhiteColor;
        _listView.delegate=self;
        _listView.dataSource = self;
        _listView.scrollEnabled = YES;
        [_listView registerClass:[ReleaseImageItem class] forCellWithReuseIdentifier:NSStringFromClass(ReleaseImageItem.class)];
    }
    return _listView;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[[NSMutableArray alloc] init];
    }
    return _dataArray;
}

@end
